package com.systop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systop.domain.vo.GoodsStockVo;
import com.systop.entity.Classify;
import com.systop.entity.Goods;
import com.systop.dao.GoodsDao;
import com.systop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    //查询商品信息
    public List<Goods> showGoodsInfo(Goods goods){
        return goodsDao.showGoodsInfo(goods);
    };
    //查询符合条件数据总数
    public int countGoods(Goods goods){
        return goodsDao.countGoods(goods);
    };

    //前台首页随机获取商品信息
    public List<Goods> getRandGoods(){
        return goodsDao.getRandGoods();
    };
    //前台首页获取最新商品信息
    public List<Goods> getNewGoods(){
        return goodsDao.getNewGoods();
    };
    //前台分类页获取所有商品分类信息
    public List<Goods> getAllClassAndGoodsInfo(){
        return goodsDao.getAllClassAndGoodsInfo();
    };

    //添加修改下拉框数据
    public List<Classify> findAllClassifyName(){
        return goodsDao.findAllClassifyName();
    };

    //添加数据查重
    public int findGoodsBygoodsName(Goods goods){
        return goodsDao.findGoodsBygoodsName(goods);
    };
    //添加商品数据
    public int addGoods(Goods goods){
        return goodsDao.addGoods(goods);
    };

    //查询订单表中是否存在相应商品订单
    public int findOrder(Goods goods){
        return goodsDao.findOrder(goods);
    };
    //删除数据方法
    public int delGoods(Goods goods){
        return goodsDao.delGoods(goods);
    };

    //修改数据查重
    public int findDataUnlessId(Goods goods){
        return  goodsDao.findDataUnlessId(goods);
    };
    //更新数据方法
    public int udpGoods(Goods goods){
        return goodsDao.udpGoods(goods);
    }

    //根据商品ID查询商品是否存在
    @Override
    public boolean goodsIsExist(List<GoodsStockVo> goodsStockVoList){
        return goodsStockVoList.size() == this.findGoodsByGoodsIds(goodsStockVoList).size();
    }

    //根据商品ID批量查询商品信息
    @Override
    public List<Goods> findGoodsByGoodsIds(List<GoodsStockVo> goodsStockVoList) {
        List<Long> ids = new ArrayList<>();
        for (GoodsStockVo goodsStockVo : goodsStockVoList){
            ids.add(goodsStockVo.getGoodsId());
        }
        return this.listByIds(ids);
    }

    @Override
    public Goods getGoodsById(Long goodsId) {
        return goodsDao.getGoodsById(goodsId);
    }

    //查询商品最新价格
    @Override
    public BigDecimal queryPrice(Long goodsId){
        return new BigDecimal(this.getGoodsById(goodsId).getPrice());
    }
}
